<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 数据统计
 */
class Cron_CNY_REPORT extends CI_Controller {
	private $cfdClass;
	public function __construct(){ 
        parent::__construct();
        if(!is_cli()){
        	header("HTTP/1.0 404 Not Found");
        	show_error('Not Found',404);
        	die;
        }

		$this->load->config('cfd');
		$this->load->library('cfdClass');
		$this->cfdClass = $this->cfdclass;
    }

	public function dst(){
		$symbol	=	"DAOUSD";
		if(!$this->cfdClass->setProduct($symbol,'IB')){//设置产品
			die("无效操作！");
		}
		$productData	=	$this->cfdClass->getProductData();
		$settlementArr	=	explode(',',$productData['settlement']);
		$dst			=	$this->cfdClass->checkDST($symbol);
		$settlement_H	=	date('H',strtotime($settlementArr[$dst]));
		$in_arr			=	array(
			'date'			=>	date("Y-m-d"),
			'settlement_H'	=>	$settlement_H,
			'datetime'		=>	date("Y-m-d H:i:s"),
		);
		$_db		=	$this->load->database('admin',true);
		$_db->insert("report_dst",$in_arr);
	}

	public function trade(){
		$this->load->model('cfd_model');
		$this->load->library('Statistics');
		$this->load->library('Referrals');

		$symbol	=	"DAOUSD";
		if(!$this->cfdClass->setProduct($symbol,'IB')){//设置产品
			die("无效操作！");
		}
		$productData = $this->cfdClass->getProductData();
		$settlementArr = explode(',',$productData['settlement']);
		$dst = $this->cfdClass->checkDST($symbol);
		$cur_H			=	date('H');
		$settlement_H	=	date('H',strtotime($settlementArr[$dst]));
		if($cur_H !=$settlement_H){
			die("结算时间：".$settlement_H);
		}

		$date		=	'2016-08-01';
		$now_date	=	date("Y-m-d",strtotime("-1 days"));
		$_db		=	$this->load->database('admin',true);
		while($date <= $now_date){
			$dst			=	$this->statistics->getDst($date);
			if(empty($dst)){
				die("结算时间获取不到！");
			}
			$settlement_H	=	$dst['settlement_H'];
			$sdate			=	date("Y-m-d ".($settlement_H+1).":00:00",strtotime($date));
			$edate			=	date("Y-m-d ".$settlement_H.":59:59",strtotime($date)+24*3600);
			echo $sdate."-".$edate."\n";
			
			$data				=	array();
			$trade				=	array();
			$trade['source_in']	=	array('10001','50001');
			
			//人民币开仓
			$kai_data			=	$this->cfd_model->getTradeStatistics($trade,$sdate,$edate);
			if(!empty($kai_data))foreach($kai_data as $items){
				if($items['dtype']=='cny'){
					$fee	=	bcmul($items['pre_open_comm'],$items['num'],2);
					$data[$items['uid']]['cny']	=	isset($data[$items['uid']]['cny'])?bcadd($data[$items['uid']]['cny'],$items['cash'],2):$items['cash'];
					$data[$items['uid']]['fee']	=	isset($data[$items['uid']]['fee'])?bcadd($data[$items['uid']]['fee'],$fee,2):$fee;
					$data[$items['uid']]['num']	=	isset($data[$items['uid']]['num'])?bcadd($data[$items['uid']]['num'],$items['num']):$items['num'];
				}else{
					$data[$items['uid']]['ticket_cny']	=	isset($data[$items['uid']]['ticket_cny'])?bcadd($data[$items['uid']]['ticket_cny'],$items['cash'],2):$items['cash'];
					$data[$items['uid']]['ticket_num']	=	isset($data[$items['uid']]['ticket_num'])?bcadd($data[$items['uid']]['ticket_num'],$items['num']):$items['num'];
				}
			}


			$trade				=	array();
			$trade['source_in']	=	array('10004','50004','10007','10009');
			$ping_data			=	$this->cfd_model->getTradeStatistics($trade,$sdate,$edate);
			if(!empty($ping_data))foreach($ping_data as $items){
				if($items['dtype']=='cny'){
					$data[$items['uid']]['yk']			=	isset($data[$items['uid']]['yk'])?bcadd($data[$items['uid']]['yk'],$items['yk'],2):$items['yk'];
				}else{
					$data[$items['uid']]['ticket_yk']	=	isset($data[$items['uid']]['ticket_yk'])?bcadd($data[$items['uid']]['ticket_yk'],$items['yk'],2):$items['yk'];
				}
			}

			$datetime	=	date("Y-m-d H:i:s");
			if(!empty($data))foreach($data as $uid=>$items){
				$master_arr	=	$this->referrals->getInviteBySlave($uid);
				$master		=	!empty($master_arr)?$master_arr['master']:"0";
				$in_arr	=	array(
					'uid'			=>	$uid,
					'master'		=>	$master,
					'cny'			=>	empty($items['cny'])?0:$items['cny'],
					'ticket_cny'	=>	empty($items['ticket_cny'])?0:$items['ticket_cny'],
					'num'			=>	empty($items['num'])?0:$items['num'],
					'ticket_num'	=>	empty($items['ticket_num'])?0:$items['ticket_num'],
					'fee'			=>	empty($items['fee'])?0:$items['fee'],
					'yk'			=>	empty($items['yk'])?0:$items['yk'],
					'ticket_yk'		=>	empty($items['ticket_yk'])?0:$items['ticket_yk'],
					'date'			=>	$date,
					'datetime'		=>	$datetime,
				);

				$_db->insert("report_user_trade",$in_arr);

				$sql	=	"select sum(cny) as cny,sum(ticket_cny) as ticket_cny,sum(num) as num,sum(ticket_num) as ticket_num,sum(fee) as fee,sum(yk) as yk,sum(ticket_yk) as ticket_yk from cfd_report_user_trade where uid='$uid'";
				$query	=	$_db->query($sql);
				$result	=	$query->row_array();
				$in_arr	=	array(
					'uid'			=>	$uid,
					'master'		=>	$master,
					'cny'			=>	empty($result['cny'])?0:$result['cny'],
					'ticket_cny'	=>	empty($result['ticket_cny'])?0:$result['ticket_cny'],
					'num'			=>	empty($result['num'])?0:$result['num'],
					'ticket_num'	=>	empty($result['ticket_num'])?0:$result['ticket_num'],
					'fee'			=>	empty($result['fee'])?0:$result['fee'],
					'yk'			=>	empty($result['yk'])?0:$result['yk'],
					'ticket_yk'		=>	empty($result['ticket_yk'])?0:$result['ticket_yk'],
					'datetime'		=>	$datetime,		
				);

				$_db->replace("report_user_tradeTotal",$in_arr);
			}

			$date		=	date("Y-m-d",strtotime($date)+24*3600);
		}
		echo "over";
    }
}
